import { createSlice } from "@reduxjs/toolkit";

const counterStore = createSlice({
    // 命名空间
    name: "counter",
    // 初始化状态
    initialState: {
        count: 0,
    },
    // 修改状态的方法 同步方法 支持直接修改
    reducers: {
        increment: (state) => {
            state.count += 1;
        },
        decrement: (state) => {
            state.count -= 1;
        },
        addToNum(state, action) {
            state.count = action.payload;
        }
    }
})

// 解构出来的方法
const { increment, decrement, addToNum } = counterStore.actions;
// 获取reducer
const reducer = counterStore.reducer;

// 以按需导出的方式导出actionCreater
export { increment, decrement, addToNum }
// 以默认导出的方式导出reducer
export default reducer;